//
//  HMUserAccount.swift
//  WeiBo14
//
//  Created by HM on 16/3/5.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit

class HMUserAccount: NSObject, NSCoding {
    
    /**
    {
    "access_token": "ACCESS_TOKEN",
    "expires_in": 1234,
    "remind_in":"798114",
    "uid":"12341234"
    }

    */

    // 用户的id
    var uid: String?
    // 生命周期(秒数) --> 从accessToken获取回来的那一刻开始计算, expires_in 这么多秒之后过期
    var expires_in: Double = 0 {
        didSet{
            // expiresDate = 当前时间 + expires_in(秒)
            expiresDate = NSDate().dateByAddingTimeInterval(expires_in)
        }
    }
    
    // 代表过期时间
    var expiresDate: NSDate?
    
    // 访问令牌
    var access_token: String?
    // 过期时间
    var remind_in: String?
    // 当前用户的昵称
    var screen_name: String?
    // 用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    
    // MARK: - 归档解档协议方法
    
    
    // 归档.告诉系统怎么存
    func encodeWithCoder(encoder: NSCoder) {
        /**
        // 用户的id
        var uid: String?
        // 生命周期(秒数)
        var expires_in: Int = 0
        // 访问令牌
        var access_token: String?
        // 过期时间
        var remind_in: String?
        // 当前用户的昵称
        var screen_name: String?
        // 用户头像地址（大图），180×180像素
        var avatar_large: String?
        */
        encoder.encodeObject(uid, forKey: "uid")
        encoder.encodeObject(access_token, forKey: "access_token")
        encoder.encodeObject(remind_in, forKey: "remind_in")
        encoder.encodeObject(screen_name, forKey: "screen_name")
        encoder.encodeObject(avatar_large, forKey: "avatar_large")
        encoder.encodeDouble(expires_in, forKey: "expires_in")
        encoder.encodeObject(expiresDate, forKey: "expiresDate")
    }
    
    
    // 解档
    required init?(coder decoder: NSCoder) {
        uid = decoder.decodeObjectForKey("uid") as? String
        access_token = decoder.decodeObjectForKey("access_token") as? String
        remind_in = decoder.decodeObjectForKey("remind_in") as? String
        screen_name = decoder.decodeObjectForKey("screen_name") as? String
        avatar_large = decoder.decodeObjectForKey("avatar_large") as? String
        expires_in = decoder.decodeDoubleForKey("expires_in")
        expiresDate = decoder.decodeObjectForKey("expiresDate") as? NSDate
    }
    
    // MARK: - des
    
    
    // 没有智能提示, 重写类似于 OC 中的 description 的 get方法
    override var description: String {
        let keys = ["uid","expires_in", "access_token"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    
    
}
